/**
 * Created by zhuyufeng on 2016/1/2.
 */
//下拉框选项
var commithouseStore = Ext.create('Ext.data.Store', {
	fields: ['house'],
	data : [
		{"house":"A111"},
		{"house":"d221"},
		{"house":"S123"}
	]
});
//下拉框
var commithouse = new Ext.form.ComboBox({
	fieldLabel: '请选择寝室',
	store: commithouseStore,
	valueField: 'house',
	tpl: Ext.create('Ext.XTemplate',      //下拉框内容
		'<tpl for=".">',
		'<div class="x-boundlist-item">{house}</div>',
		'</tpl>'
	),
	displayTpl: Ext.create('Ext.XTemplate', 		// 输入框显示内容
		'<tpl for=".">',
		'{house}',
		'</tpl>'
	)
});
Ext.define('commitModel', {
	extend: 'Ext.data.Model',
	fields: [ 'check','num','msg']
});
var commitStore = Ext.create('Ext.data.Store', {
	model: 'commitModel',
	autoLoad: true,
	pageSize: 10,
	proxy: {
		type: 'ajax',
		url : './json/commit',
		reader: {
			type: 'json',
			root: 'rows',
			totalProperty: 'total'
		}
	}
});
var commitsel;
function commitdel(obj){
	if(obj == ''){
		Ext.MessageBox.alert('提示','请选中要删除的记录');
	}else {
		for (var p in obj) {
			commitGrid.store.remove(obj[p]);
		}
	}
}
var commitGrid = Ext.create('Ext.grid.Panel', {
	store: commitStore,
	height:180,
	forceFit:true,
	selType: 'checkboxmodel',
	selModel: new Ext.selection.CheckboxModel({
		injectCheckbox:0,//checkbox位于哪一列，默认值为0
		mode:'simple',//multi,simple,single；默认为多选multi
		allowDeselect:true,//如果值true，并且mode值为单选（single）时，可以通过点击checkbox取消对其的选择
		checkOnly:false,//当checkOnly的值为true时只能从前面的复选框来选中本行，为false则可以
		enableKeyNav:false,
		listeners: {
			deselect: function (model, record, index) {//取消选中时产生的事件
			},
			select: function (model, record, index) {//record被选中时产生的事件
			},
			selectionchange: function (model, selected) {//选择有改变时产生的事件
				var records = model.getSelection();
				if (records != '') {
					for (var i in records) {
						records[i].get('name')//选中的节目名称（方法一）
					}
				}
				if (selected != '') {
				}
				commitsel = selected;
			}
		}
	}),
	columns: [
		{text: '寝室号',width: 100,dataIndex: 'num'},
		{text: '违规信息',width: 150,dataIndex: 'msg'}
	],
	dockedItems:[{
		xtype:'pagingtoolbar',
		store:commitStore,
		dock:'bottom',
		displayInfo:true,
		displayMsg:'第{0}条到第{1}条,一共{2}条',
		emptyMsg:'没有记录'
	}],
	tbar:[{
		xtype:'button',
		text:'添加',
		handler:function(){
			var commitObj = commit.items.items[1].lastValue;
			var key = commit.items.items[0].rawValue;
				if(commitObj==''){
					alert('请输入违规信息');
				}else{
					var a={
						'num':key,
						'msg':commitObj
					};
					commitStore.add(a);
			}
		}
	},{
		text:'删除',
		handler:function(){
			commitdel(commitsel);
		}
	}],
	bbar:[{
		xtype:'button',
		text:'发布',
		buttonAlign:'center',
		handler:function(){}
	}]
});

var commit = new Ext.Panel({
	region:'center',
	split:false,
	border:true,
	collapsible:false,
	activeTab:0,
	autoHeight:true,
	title:'发布公告',
	items:[commithouse,{
		xtype: 'htmleditor',
		fieldLabel: '请填写违规信息',
		width:500
	},commitGrid]
})